﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows;

namespace MauThuTabControl
{
    class ConnectionDB
    {
        SqlConnection con = new SqlConnection();
        public  int tag = 0;
        
        public ConnectionDB()
        {
            con.ConnectionString = ("Data Source=.\\SQLEXPRESS;Initial Catalog=SaleManager;Integrated Security=True");
            if (con.State == ConnectionState.Closed)
                con.Open();
        }
        

        public DataTable LoadData(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, con);
            //cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            //dt.TableName = "" + table + "";
            da.Fill(dt);
            cmd.Dispose();
            da.Dispose();
            dt.Dispose();
            return dt;
        }

        public bool _ThucThiCauTruyVan(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd = new SqlCommand(sql, con);
                cmd.ExecuteNonQuery();//thực thi SqlCommand ko trả về giá trị
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); ; return false; }
            return true;
        }

        public DataTable Loadtree(string sql)
        {
            SqlCommand cmd = new SqlCommand(sql, con);
            //cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            //dt.TableName = "" + table + "";
            da.Fill(dt);

            return dt;
        }

        public string _LayTruongKieuChuoi(string sql)
        {
            string t = "";
            SqlCommand cmd = new SqlCommand(sql, con);
            try
            {
                //thực thi SqlCommand để trả về một giá trị
                t = Convert.ToString(cmd.ExecuteScalar());
            }
            catch { t = ""; }
            return t;
        }
    }
}
